package com.lanchewuyou.user.driver.push;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.igexin.sdk.PushConsts;
import com.lanchewuyou.user.driver.R;
import com.lanchewuyou.user.driver.Utils.NetUtils;
import com.lanchewuyou.user.driver.lipeiui.LiPeiMainActivity;
import com.lanchewuyou.user.driver.modul.Config;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 推送接受广播(使用第三方推送-个推,具体见个推官网)
 */
public class PushReceiver extends BroadcastReceiver {

    private String orderid;
    private String driverid;
    private String mobile;

    private String savemobile, saveid;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d("GetuiSdkDemo", "onReceive() action=" + bundle.getInt("action"));
        SharedPreferences sharedPreferences = context.getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
        savemobile = sharedPreferences.getString("mobile", "");
        saveid = sharedPreferences.getString("driverid", "");
        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_MSG_DATA:
                // 获取透传（payload）数据
                byte[] payload = bundle.getByteArray("payload");
                if (payload != null) {
                    String data = new String(payload);
                    try {
                        JSONObject jsonObject = new JSONObject(data);
                        orderid = jsonObject.getString("orderid");
                        driverid = jsonObject.getString("driverid");
                        mobile = jsonObject.getString("mobile");
                        Log.d("json", orderid + " " + driverid + " " + mobile);
                        if (driverid.equals(saveid) && mobile.equals(savemobile)) {
                            if (NetUtils.isRunningForeground(context)) {
                                NotificationManager notificationManager = (NotificationManager)
                                        context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
                                Notification notification = new Notification(R.mipmap.type_icon_red, "有新的易理赔订单", System.currentTimeMillis());
                                notification.setLatestEventInfo(context, "易理赔", "您好，有新的易理赔订单", null);
                                notification.defaults = Notification.DEFAULT_ALL;
                                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                                notificationManager.notify(1, notification);
                            } else {
                                NotificationManager notificationManager = (NotificationManager)
                                        context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
                                Notification notification = new Notification(R.mipmap.type_icon_red, "有新的易理赔订单", System.currentTimeMillis());
                                Intent i = new Intent(context, LiPeiMainActivity.class);
                                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
                                notification.setLatestEventInfo(context, "易理赔", "您好，有新的易理赔订单", contentIntent);
                                notification.defaults = Notification.DEFAULT_ALL;
                                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                                notificationManager.notify(1, notification);
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                break;
            default:
                break;
        }
    }
}
